package com.yunxin.utils;



import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

public class StringFormat {







    /**
     * 模仿C#格式化字符串
     *
     * @param str
     * @param args
     * @return
     */
    public static String format(String str, String... args) {
        for (int i = 0; i < args.length; i++) {
            str = str.replaceFirst("\\{\\}", args[i]);
        }
        return str;
    }

    public static String format(String str, Object... args) {
        for (int i = 0; i < args.length; i++) {
            str = str.replaceFirst("\\{\\}", String.valueOf(args[i]));
        }
        return str;
    }




    /**
     * 安全toString
     *
     * @param str
     * @return
     */
    public static String toString(Object str) {
        if (null == str)
            return null;
        return str.toString();
    }

    /**
     * 通过","链接List
     *
     * @param collection
     * @return
     */
    public static String joinBy(String splitter, Collection<String> collection) {
        return collection.stream().filter(s->s!=null).collect(Collectors.joining(splitter));
    }

    /**
     * 将数组轻而易举的转换成用逗号分隔的字符串
     *
     * @param strs
     * @return
     */
    public static String joinBy(String splitter,String... strs) {
        return StringFormat.joinBy(true,splitter, strs);
    }

    /**
     * 将数组轻而易举的转换成用逗号分隔的字符串
     *
     * @param strs
     * @return
     */
    public static String joinBy(boolean filterNull, String splitter, String... strs) {
        if(strs==null){
            return "";
        }else{
            return Arrays.stream(strs).filter(s->s!=null).collect(Collectors.joining(splitter));
        }

    }




    public static void main(String[] args) {
        String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };
        System.out.println(StringFormat.joinBy("\t",stringArray));

        String unitId="unitId";
        Integer sort=0;
        String parentIds="parentIds";
        String str = StringFormat.format("查询原题对应的衍生题param,,unitId={},sort={},parentIds={}", unitId, sort, parentIds);
        System.out.println(str);
    }
}